package com.animal.service;

import com.animal.mapper.ItemCatMapper;
import com.animal.pojo.ItemCat;
import com.animal.vo.EasyUITree;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ItemCatServiceImpl implements ItemCatService{

    @Autowired
    private ItemCatMapper itemCatMapper;

    @Override
    public ItemCat findItemCatById(Long id) {

         return itemCatMapper.selectById(id);
    }

    @Override
    public List<EasyUITree> findItemCatList(long parentId) {
        QueryWrapper<ItemCat> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        List<ItemCat> itemCatList= itemCatMapper.selectList(queryWrapper);
        List<EasyUITree> voList=new ArrayList<>(itemCatList.size());
        for(ItemCat itemCat:itemCatList){
            long id=itemCat.getId();
            String text=itemCat.getName();
            String state=itemCat.getIsParent()? "closed":"open";
            EasyUITree tree=new EasyUITree(id,text,state);
            voList.add(tree);
        }
        return voList;
    }
}
